package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ArticleReplyDao;
import com.cqut.forum.entity.ArticleReply;
import com.cqut.forum.service.ArticleReplyService;

/**
 * 评论接口实现类
 * @author yangxun
 */
@Component
public class ArticleReplyServiceImp implements ArticleReplyService {
	
	@Resource
	private ArticleReplyDao articleReplyDao;
	@Override
	public ArticleReply save(ArticleReply articleReply) {
		return articleReplyDao.save(articleReply);
	}
	@Override
	public List<Map<String, Object>> getByOwnerId(int userId,int index,int size) {
		List<Map<String, Object>> replys = articleReplyDao.getByOwnerId(userId,index,size);
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String,Object> m:replys){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userPhoto", m.get("user_photo"));
			map.put("userName", m.get("user_name"));
			map.put("replyContent", m.get("article_reply_contents"));
			map.put("replyTime", m.get("article_reply_time"));
			map.put("articleId", m.get("article_id"));
			result.add(map);
		}
		return result;
	}
	/**
	 * 获取一篇博客的所有评论
	 */
	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getAticleReplys(int articleId) {
		List<Map<String, Object>> replys = articleReplyDao.getArticleReplys(articleId, 0, 1000);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:replys){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userPhoto", m.get("user_photo"));
			map.put("userName", m.get("user_name"));
			map.put("replyContent", m.get("article_reply_contents"));
			map.put("replyId", m.get("article_reply_id"));
			map.put("replyDate", ((Date)m.get("article_reply_time")).toLocaleString());
			
			
			result.add(map);
		}
		
		return result;
	}
	@Override
	public boolean deleteById(int replyId) {
		articleReplyDao.delete(replyId, ArticleReply.class);
		return true;
	}

}
